﻿Imports MySql.Data.MySqlClient
Public Class EditCourse
    Public SQL As MySqlConnection
    Public newCourseid As String
    Public key As String
    Public Sub New(ByRef SQLCon As MySqlConnection, ByVal Cour_id As String)
        InitializeComponent()
        SQL = SQLCon
        newCourseid = Cour_id
        'MsgBox(newCourseid)
    End Sub
    Private Sub EditCourse_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Showdata()
    End Sub
    Private Sub Showdata()
        '---------------------------------------------Check Start date couse---------------------------
        '---------------------------------------------End Check Start date couse---------------------------

        If SQL.State = ConnectionState.Closed Then
            SQL.Open()
        End If

        key = newCourseid

        Dim mysqlda As New MySql.Data.MySqlClient.MySqlDataAdapter
        Dim mysqldr As MySql.Data.MySqlClient.MySqlDataReader

        Dim comsql As String = "SELECT * FROM Course where course_id = " + newCourseid + ";"
        Dim adater As New MySqlCommand


        Try
            adater.CommandText = comsql
            adater.Connection = SQL
            mysqlda.SelectCommand = adater
            mysqldr = adater.ExecuteReader
            While (mysqldr.Read())
                txt_CourseName.Text = mysqldr("course_name")
                txt_CourseCost.Text = mysqldr("course_cost")
                txt_Trainer.Text = mysqldr("course_trainer")
                txt_CourseDuration.Text = mysqldr("course_duration")
                DateTimePicker1.Text = mysqldr("course_start_date")
            End While
            mysqldr.Close()
            'Catch ex As MySqlException
            'MsgBox(ex.Message)
        Catch
        End Try
        SQL.Close()

    End Sub
    Private Sub UpdateData()
        Dim commandText2 As String
        Dim mySqlCommand As New MySqlCommand
        If SQL.State = ConnectionState.Closed Then
            SQL.Open()
        End If
        commandText2 = "UPDATE Course SET course_name = '" & txt_CourseName.Text & "' , course_cost = '" & txt_CourseCost.Text & "' , course_trainer = '" & txt_Trainer.Text & "' , course_trainer = '" & txt_Trainer.Text & "' , course_start_date = '" & DateTimePicker1.Value.Year.ToString() & "-" & DateTimePicker1.Value.Month.ToString() & "-" & DateTimePicker1.Value.Day.ToString() & "' , course_duration = '" & txt_CourseDuration.Text & "' WHERE course_id = " & newCourseid & "; "
        MySqlCommand.CommandText = commandText2
        MySqlCommand.CommandType = CommandType.Text
        MySqlCommand.Connection = SQL

        MySqlCommand.ExecuteNonQuery()
        SQL.Close()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        UpdateData()
        Showdata()
    End Sub

    Private Sub BackButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BackButton.Click
        Dim mainForm As addgroup2_1 = New addgroup2_1(SQL)
        mainForm.Show()

        Me.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Showdata()
    End Sub
End Class